package com.aaa.utils;

import com.aaa.vo.Product;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * jdhtml解析
 *
 * @author 太阳偷渡青山
 * @date 2022/12/28
 */
public class JDHtmlParse {
    public static List<Product> parse(String keyword) throws Exception {
        String path="https://search.jd.com/Search?keyword="+keyword;
        URL url=new URL(path);
        //Document:其实就是当前京东的网页对象
        Document document = Jsoup.parse(url, 30000);

        //获取京东商品标签--对象
        Element j_goodsList = document.getElementById("J_goodsList");

        //获取所有的li标签
        Elements li = j_goodsList.getElementsByTag("li");
        List<Product> products=new ArrayList<>();
        for(Element e:li){
            String pname = e.getElementsByClass("p-name").get(0).text();
            String pprice = e.getElementsByClass("p-price").get(0).text();
            String imgUrl = e.getElementsByTag("img").get(0).attr("data-lazy-img");
            Product product=new Product(pname,pprice,imgUrl);
            products.add(product);
        }

        return products;

    }
}
